package top.marken.onlineclass.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.marken.onlineclass.util.JsonData;

import java.time.LocalDateTime;

@ControllerAdvice
public class ExceptionGlobalHandler {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handler(Exception e) {
        logger.error("[时间：" + LocalDateTime.now() + "] 全局异常：\n" + e);

        if (e instanceof ProjectException) {
            ProjectException projectException = (ProjectException) e;
            return new JsonData(projectException.getCode(),e.toString(),projectException.getMsg());
        } else {
            return new JsonData(JsonData.CODE_ERROR,e.toString(),"全局异常，未知错误！");
        }
    }
}
